<template>
	<!-- 
		解决闪烁问题
			当数据没有返回时，价格会显示undefined（数据未返回，goods_info为空，goods_info.goods_price为undefined）
	 -->
	<view v-if="goods_info.goods_price" class="goods-detail-container">
		<!-- 轮播图 -->
		<swiper class="swiper" :indicator-dots="true" :autoplay="true" circular="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item, index) in goods_info.pics" :key="index">
					<image :src="item.pics_big" @click="preview(index)"></image>
			</swiper-item>
		</swiper>
		
		<!-- 商品信息 -->
		<view class="goods-info-box">
			
			<view class="goods-price">
				￥{{goods_info.goods_price}}
			</view>
			<view class="goods-info-body">
				<view class="goods-name">
					{{goods_info.goods_name}}
				</view>
				<view class="goods-favi">
					<uni-icons type="star" size="20"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="cost">
				快递：免运费
			</view>
		</view>
		
		<!-- 商品介绍 -->
		<rich-text :nodes="goods_info.goods_introduce"></rich-text>
		
		<!-- 商品组件导航 -->
		<view class="goods-nav">
			<!-- fill="true" 控制右侧按钮的样式 -->
			<!-- options 左侧按钮配置项 -->
			<!-- buttonGroup 右侧按钮配置向 -->
			<!-- @click左侧按钮事件处理函数 -->
			<!-- @buttonClick 右侧按钮事件处理函数 -->
			<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import { mapState, mapMutations, mapGetters} from 'vuex'
	export default {
		computed: {
			// state,getters在computed中映射
			...mapState('m_cart', ['cart']),
			...mapGetters('m_cart', ['total'])
		},
		data() {
			return {
				// 商品详情页
				goods_info: {},
				// 左侧按钮配置对象
				options: [
					{
						text: '店铺',
						icon: 'shop'
					},
					{
						text: '购物车',
						icon: 'cart',
						info: 0
					}
				],
				// 右侧按钮配置对象
				buttonGroup: [
					{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			};
		},
		
		methods: {
			// mapMutations在methods中映射
			...mapMutations('m_cart',['addToCart']),
			
			// 获取商品详情页
			async getGoodsInfo (goods_id) {
				const {data: res} = await uni.$http.get('/api/public/v1/goods/detail', {goods_id})
				// console.log(res);
				if(res.meta.status !== 200) return uni.$showMsg()
				// 解决图片留白问题（第一个replace）和ios不显示图片问题（第一个replace），ios不显示webp图片
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display: block;"').replace(/.webp/g, '.jpg')
				this.goods_info = res.message
			},
			
			// 图片预览
			preview(index) {
				uni.previewImage({
					current: index,
					urls:this.goods_info.pics.map(item => item.pics_big)
				})
			},
			
			// 跳转至购物车页面
			onClick(e) {
				// console.log(e);
				if(e.content.text === '购物车') {
					uni.switchTab({
						url:'/pages/cart/cart'
					})
				}
			},
			
			// 加入购物车
			buttonClick(e) {
				// console.log(e);
				if(e.content.text === '加入购物车'){
					const goods = {
						goods_id: this.goods_info.goods_id,
						goods_name: this.goods_info.goods_name,
						goods_count: 1,
						goods_price: this.goods_info.goods_price,
						goods_samll_logo: this.goods_info.goods_samll_logo,
						goods_state: true
					}
					// 调用mutations映射的方法
					this.addToCart(goods)
				}
				
			}
		},
		
		onLoad(options) {
			// console.log(options);
			this.getGoodsInfo(options.goods_id)
		},
		
		// 监听器,普通函数的侦听器首次加载页面不会显示
		watch: {
			// 监听购物车数量的变化
			// total(newTotal) {
			// 	// 通过find()方法找到info
			// 	const result = this.options.find(item => item.text === '购物车')
			// 	if(result) {
			// 		result.info = newTotal
			// 	}
			// }
			// 监听购物车数量的变化
			total: {
				// handler作为侦听器的处理函数
				handler(newTotal) {
					const result = this.options.find(item => item.text === '购物车')
					if(result) {
						result.info = newTotal
					}
				},
				// 在页面首次在加载时开始监听
				immediate: true
			}
		}
	}
</script>

<style lang="scss">
	.goods-detail-container {
		padding-bottom: 50px;
	}
.swiper {
	height: 810rpx;
	image {
		width: 100%;
		height: 100%;
	}
}
.goods-info-box {
	padding: 10px 0 10px 10px;
	.goods-price {
		color: #c00000;
		font-size: 18px;
		margin: 10px 0;
	}
	.goods-info-body {
		display: flex;
		justify-content: space-between;
		.goods-name {
			font-size: 13px;
			padding-right: 10px;
		}
		.goods-favi {
			width: 120px;
			font-size: 12px;
			display: flex;
			flex-direction: column;
			align-items: center;
			border-left: 1px solid #efefef;
			color: gray;
		}
	}
	.cost {
		margin: 10px 0;
		color: gray;
		font-size: 12px;
	}
}
.goods-nav {
	position: fixed;
	left: 0;
	bottom: 0;
	width: 100%;
}
</style>
